package com.example.kaoqinmanager.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.kaoqinmanager.R;
import com.example.kaoqinmanager.activity.LoginActivity;
import com.example.kaoqinmanager.bean.PaiBanTable;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import org.litepal.LitePal;

import java.util.List;

/**
 * @author ypk
 * 创建日期：2021/12/15  10:53
 * 描述：
 */
public class PaiBanAdapter extends RecyclerView.Adapter<PaiBanAdapter.BulletinViewHolder> {

    private Context context;
    List<PaiBanTable> list;

    public PaiBanAdapter(Context context, List<PaiBanTable> list) {
        this.context = context;
        this.list = list;
    }

    public void setNewData(List<PaiBanTable> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public BulletinViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_paibai, parent, false);
        return new BulletinViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull BulletinViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvName.setText(list.get(position).getName());
        holder.tvStartTime.setText(list.get(position).getStartTime());
        holder.tvEndTime.setText(list.get(position).getEndTime());

        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete(position);
            }
        });
    }

    private void delete(int position) {

        new MaterialAlertDialogBuilder(context)
                .setTitle("提示")
                .setMessage("您确定要删除吗？")
                .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        PaiBanTable paiBanTable = list.get(position);
                        LitePal.delete(PaiBanTable.class, paiBanTable.getId());
                        list.remove(position);
                        notifyDataSetChanged();
                    }
                })
                .setPositiveButton("取消", null)
                .show();


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class BulletinViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private TextView tvStartTime;
        private TextView tvEndTime;
        private TextView tvDelete;

        public BulletinViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvStartTime = itemView.findViewById(R.id.tvStartTime);
            tvEndTime = itemView.findViewById(R.id.tvEndTime);

            tvDelete = itemView.findViewById(R.id.tvDelete);
        }
    }


}
